﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Engine;
using Microsoft.Xna.Framework;
using PlaytimeSaveTypes;

namespace Playtime
{
    public class JointAnimationContainer : Component
    {
        public List<JointAnimation> Animations;

        JointAnimation activeAnimation = null;
        List<string> files;

        public JointAnimationContainer()
        {
            Animations = new List<JointAnimation>();
        }

        public JointAnimationContainer(List<string> _files)
        {
            Animations = new List<JointAnimation>();
            files = _files;
        }

        public override void  Awake()
        {
            JointStructure structure = gameObject.GetComponent<JointStructure>();
 	        foreach (string file in files)
            {
                Animations.Add(new JointAnimation(XMLHandler.Deserialize<JointAnimationSaveData>(file), structure, "Run"));
            }
        }

        public void Play(string _name)
        {
            for (int i = 0; i < Animations.Count; i++)
            {
                if (Animations[i].Name == _name)
                {
                    activeAnimation = Animations[i];
                    activeAnimation.IsPlaying = true;
                    break;
                }
            }
        }

        public void Pause()
        {
            if(activeAnimation != null)
                activeAnimation.IsPlaying = false;
        }

        public override void Update(GameTime _gameTime)
        {
            base.Update(_gameTime);
            if (activeAnimation != null)
                activeAnimation.Update(_gameTime);
        }
    }
}
